package com.plm.mqshequ.utils;


import org.apache.shiro.crypto.hash.SimpleHash;

/**
 * 加密工具
 * @author Limi
 * @create 2019-01-10 0:24
 */
public class EncryptUtils {

    /**
     * @param hashAlgorithmName 加密方式
     * @param credentials 明文密码
     * @param salt 盐值(需唯一)，通过加盐处理，即使明文密码一样，加密后的字符串都不一样
     * @param hashIterations 加密的次数
     * @return String
     */
    private static String encrypt (String hashAlgorithmName, String credentials,
                                   String salt, int hashIterations) {
        Object simpleHash = new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);
        return simpleHash.toString();
    }

    /**
     * MD2盐值加密
     */
    public static String MD2(String password, String salt, int hashIterations) {
        return encrypt("MD2", password, salt, hashIterations);
    }

    /**
     * MD5盐值加密
     */
    public static String MD5(String password, String salt, int hashIterations) {
        return encrypt("MD5", password, salt, hashIterations);
    }

    /**
     * SHA1盐值加密
     */
    public static String SHA1(String password, String salt, int hashIterations) {
        return encrypt("SHA-1", password, salt, hashIterations);
    }

    /**
     * SHA256盐值加密
     */
    public static String SHA256(String password, String salt, int hashIterations) {
        return encrypt("SHA-256", password, salt, hashIterations);
    }


}
